package com.itheima.info.manager.dao;

import com.itheima.info.manager.domain.Student;

import java.util.ArrayList;

public class ArraylistStudentDao extends BaseStudentDao{
    static ArrayList<Student> students = new ArrayList<>();

    static {
        students.add(new Student("s001", "张三", "23", "山西"));
        students.add(new Student("s002", "赵四", "24", "东北"));
        students.add(new Student("s003", "王五", "88", "北京"));
    }

    public boolean addStudent(Student student) {
        students.add(student);
        return true;
    }

    public Student[] findAllStudent() {
        Student[] student = new Student[students.size()];
        for (int i = 0; i < student.length; i++) {
            student[i] = students.get(i);
        }
        return student;
    }

    public void deleteStudentById(String deleteId) {
        int index = getIndex(deleteId);
        students.remove(index);
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            if (null != students.get(i) && students.get(i).getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String updateId, Student newStudent) {
        int index = getIndex(updateId);
        students.set(index, newStudent);
    }
}
